﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using WebPart = System.Web.UI.WebControls.WebParts.WebPart;

namespace Havivi.FootballPool.StandingsWebPart
{
    [ToolboxItemAttribute(false)]
    public class StandingsWebPart : WebPart
    {
        private int numberOfUsers = 40;


        [WebBrowsable(true),
  WebDescription("Show number of users"),
  WebDisplayName("The maximum number of  users "),
  Personalizable(PersonalizationScope.Shared)]
        public int NumberOfUsers
        {
            get
            {
                return numberOfUsers;
            }

            set
            {
                numberOfUsers = value;
            }
        }



        protected override void CreateChildControls()
        {
            try
            {

                var web = SPContext.Current.Site.RootWeb;
                string orginalUrl = HttpContext.Current.Request.Url.AbsoluteUri;
                if (HttpContext.Current.Request.Url.Query.Length > 0)
                    orginalUrl = orginalUrl.Replace(HttpContext.Current.Request.Url.Query, string.Empty);
                var list = web.Lists["Euro 2012 Standings"];
                var query = new SPQuery
                                {
                                    Query =
                                        "<OrderBy><FieldRef Name=\"Score\" Ascending=\"False\" /></OrderBy>",
                                    RowLimit = Convert.ToUInt32(numberOfUsers),
                                    ViewFields = string.Concat("<FieldRef Name='Position' />",
                                        "<FieldRef Name='Name' />",
                                        "<FieldRef Name='Score' />"),
                                    ViewFieldsOnly = true
                                };

                DataTable items = list.GetItems(query).GetDataTable();

                if (items != null)
                {
                    var dg = new DataGrid
                                 {
                                     CssClass = "ms-listviewtable",
                                     CellPadding = 4,
                                     AllowPaging = false,
                                     GridLines = GridLines.None,
                                     Width = 300
                                 };
                    dg.AlternatingItemStyle.CssClass = "ms-alternatingstrong";
                    dg.HeaderStyle.CssClass = "ms-standardheader ms-WPTitle";
                    items.Columns[0].ColumnName = "#";
                    int index = 0;
                    foreach (DataRow row in items.Rows)
                    {
                        index++;
                        row["#"] = index;
                        row["Name"] = string.Format(Redirect, orginalUrl + "?euroUser=" + row["Name"], row["Name"]);
                        if (index == numberOfUsers)
                            break;
                    }
                    dg.DataSource = items;
                    dg.DataBind();
                    Controls.Add(dg);
                    if (items.Rows.Count >= numberOfUsers)
                        Controls.Add(new LiteralControl("<br><A class=\"ms-addnew\" href=\"" + list.DefaultViewUrl + "\"  target=\"_self\">Show all users</A>"));
                }

            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(ex.Message));
            }
        }
        private static string Redirect = "<a style=\"color: #676767\" href=\"{0}\" >{1}</a>";
    }
}
